package com.atguigu.lease.web.admin.custom.task;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;


//自定义定时任务类
@Component
public class MyTesk {

    @Autowired
    private LeaseAgreementService leaseAgreementService;


    //定时任务，十秒一次，扫描租约类型，到时间的在租状态修改为已到期
    @Scheduled(cron = " 0/10 * * * * * ")
    public void myTask(){
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.le(LeaseAgreement::getLeaseEndDate,new Date())
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.RENEWING,LeaseStatus.WITHDRAWING)
                        .set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(lambdaUpdateWrapper);
    }
}
